package com.sky.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sky.annotation.AutoFill;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.enumeration.OperationType;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private DishMapper dishMapper;
    @Override
    public void save(CategoryDTO categoryDTO) {
        Category category=new Category();
        BeanUtils.copyProperties(categoryDTO,category);
        /*公共字段*/
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        category.setCreateUser(BaseContext.getCurrentId());
        category.setUpdateUser(BaseContext.getCurrentId());
        category.setStatus(StatusConstant.DISABLE);
        categoryMapper.insert(category);
    }

    @Override
    public PageResult pageQuery(CategoryPageQueryDTO categoryPageQueryDTO) {
        IPage iPage = new Page(categoryPageQueryDTO.getPage(),categoryPageQueryDTO.getPageSize());
        LambdaQueryWrapper<Category> lam=new LambdaQueryWrapper<>();
        if(categoryPageQueryDTO.getName()!=null&&categoryPageQueryDTO.getName()!=" "){
            lam.like(Category::getName,categoryPageQueryDTO.getName());
        }
        if(categoryPageQueryDTO.getType()!=null){
            lam.eq(Category::getType,categoryPageQueryDTO.getType());
        }
        lam.orderByAsc(Category::getSort).orderByDesc(Category::getCreateTime);
        IPage iPage1 = categoryMapper.selectPage(iPage, lam);
        long total=iPage.getTotal();
        List<Category> categoryList=iPage.getRecords();
        PageResult pageResult=new PageResult(total,categoryList);
        return pageResult;
    }

    @Override
    public void deleteById(Long id) {
        LambdaQueryWrapper<Dish> lam=new LambdaQueryWrapper<>();
        lam.eq(Dish::getCategoryId,id);
        Integer count=dishMapper.selectCount(lam);
        if(count>0){
            //当前分类下有菜品，不能删除
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
        }
        LambdaQueryWrapper<Setmeal> lam2=new LambdaQueryWrapper<>();
        lam2.eq(Setmeal::getCategoryId,id);
        Integer count2=setmealMapper.selectCount(lam2);
        if(count2>0){
            //当前分类下有菜品，不能删除
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
        }
        categoryMapper.deleteById(id);
    }

    @Override
    public void update(CategoryDTO categoryDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO,category);
        /*公共字段*/
        category.setUpdateTime(LocalDateTime.now());
        category.setUpdateUser(BaseContext.getCurrentId());
        categoryMapper.updateById(category);
    }

    @Override
    public void startOrStop(Integer status, Long id) {
        Category category=new Category();
        category.setStatus(status);
        category.setId(id);
        category.setUpdateUser(BaseContext.getCurrentId());
        category.setUpdateTime(LocalDateTime.now());
        categoryMapper.updateById(category);
        LambdaQueryWrapper<Dish> lam=new LambdaQueryWrapper<>();
        lam.eq(Dish::getCategoryId,id);
        List<Dish> dishList = dishMapper.selectList(lam);
        System.out.println("数据为=====>"+dishList);
        if(dishList!=null&&dishList.size()>0 && status==0){
            dishList.stream().forEach(dish -> {
                Long dishId = dish.getId();
                dish.setId(dishId);
                dish.setStatus(status);
                dish.setUpdateUser(BaseContext.getCurrentId());
                dish.setUpdateTime(LocalDateTime.now());
                System.out.println(dishMapper.updateById(dish));
            });
        }

    }

    @Override
    public List<Category> list(Integer type) {
        LambdaQueryWrapper<Category> lam=new LambdaQueryWrapper<>();
        if(type!=null){
            lam.eq(Category::getType,type).eq(Category::getStatus,1);
        }
        lam.orderByAsc(Category::getSort).orderByDesc(Category::getCreateTime);
        List<Category> categoryList=categoryMapper.selectList(lam);
//        System.out.println("数据为=======>"+categoryList);
        return categoryList;
    }
}
